package com.guomei.controller.homeData;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.guomei.bean.homeData.domain.Banner;
import com.guomei.bean.homeData.domain.GetHomeDataDTO;
import com.guomei.bean.homeData.vo.DeleteBannerDTO;
import com.guomei.mapper.homeData.BannerMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guomei.utils.Result;
import com.guomei.utils.PageResult;

import lombok.extern.slf4j.Slf4j;



/**
 * 首页管理
 */
@RestController
@RequestMapping("/api1/homeData")
@Slf4j
public class HomeDataController {

    @Autowired
    BannerMapper bannerMapper;

    @PostMapping("/index")
    public Result<?> getHomeData(@RequestBody GetHomeDataDTO getHomeDataDTO) {
        QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
        // queryWrapper.eq("is_deleted", null).orderByDesc("id");
        if (getHomeDataDTO.getBizType() != null) {
            queryWrapper.eq("biz_type", getHomeDataDTO.getBizType());
        }
        if (getHomeDataDTO.getKeyword() != null) {
            queryWrapper.like("title", getHomeDataDTO.getKeyword());
        }
        int pageNum = getHomeDataDTO.getPageNum() == null ? 1 : getHomeDataDTO.getPageNum();
        int pageSize = getHomeDataDTO.getPageSize() == null ? 10 : getHomeDataDTO.getPageSize();

        Page<Banner> page = new Page<>(pageNum, pageSize);
        Page<Banner> resultPage = bannerMapper.selectPage(page, queryWrapper);

        List<Banner> bannerList = resultPage.getRecords();
        long total = resultPage.getTotal();
        PageResult<List<Banner>> pageResult = PageResult.success(bannerList, total, pageSize, pageNum);
        return Result.success(pageResult);
    }



    @PostMapping("/save")
    public Result<?> saveBanner(@RequestBody Banner bannerData) {
        log.info("添加菜单入参：{}", bannerData);
        if (bannerData != null && bannerData.getId() != null && bannerData.getId() > 0) {
            bannerMapper.updateById(bannerData);
        } else {
            bannerMapper.insert(bannerData);
        }

        return Result.success(bannerData);
    }

    @DeleteMapping("/deletebyIds")
    public Result<?> deleteBanner(@RequestBody List<Long> ids) {
        bannerMapper.deleteBatchIds(ids);
        return Result.success(ids);
    }
}
